home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / ControlStripShell 1.1 / CSShell.c < prev    next >
Text File  |  1996-04-01  |  6KB  |  246 lines

  1. /* ---------------------------------------------------------------------------
  2. CSShell.c
  3.  
  4.     ©1994-1996 Martin R. Wachter; All Rights Reserved.
  5.  
  6.     A sample Control Strip Module shell with a placeholder popup menu.
  7.     
  8. Version History
  9. 1.0 9/94    by Marty Wachter
  10. 1.1 4/1/96    by Marty Wachter -- cleanedup code for CW8
  11.  
  12. Based on the MacCalendar Control Strip Sample by Martin Minow, MACDTS.
  13. --------------------------------------------------------------------------- */
  14.  
  15. #include "CSShell.h"
  16.  
  17. #include <RomDefs.h>
  18. #include <Slots.h>
  19. #include <Displays.h>
  20. #include <Video.h>
  21.  
  22. // ---------------------------------------------------------------------------
  23. //        • main
  24. // ---------------------------------------------------------------------------
  25. //
  26. // main entry point into our sdev
  27. //
  28. pascal long main (long message, long params, Rect *statusRect, GrafPtr statusPort)
  29. {
  30.     long result = 0L;
  31.     Str255    helpString;
  32.     
  33.     switch (message)
  34.     {
  35.         case sdevInitModule:         // check environs, allocate globals
  36.             result = DoCSInit();
  37.         break;
  38.         
  39.         case sdevCloseModule:         // release my memory
  40.             DoCSClose((MyGlobalHandle) params);
  41.         break;
  42.         
  43.         case sdevFeatures:             // let the strip track the mouse down
  44.             result = (  (1<<sdevWantMouseClicks)    // We handle mouse down
  45.                       | (1<<sdevDontAutoTrack)        // We track the mouse, too
  46.                       | (1<<sdevHasCustomHelp)        // Custom help string
  47.                     );
  48.             break;
  49.         break;
  50.         
  51.         case sdevGetDisplayWidth:    // inform the strip how much strip space we need
  52.             result = kIconWidth + width((*(*(MyGlobalHandle) params)->myArrowPict)->picFrame);
  53.         break;
  54.         
  55.         case sdevPeriodicTickle:    // no idle time used
  56.         break;
  57.         
  58.         case sdevDrawStatus:         // draw my icon and arrow pict
  59.             DoCSDraw((MyGlobalHandle) params, statusRect, statusPort);
  60.         break;
  61.         
  62.         case sdevMouseClick:         // the mouse was clicked & released in my button
  63.             DoCSClick((MyGlobalHandle) params, statusRect);
  64.         break;
  65.         
  66.         case sdevSaveSettings:        // no settings in this module
  67.         break;
  68.         
  69.         case sdevShowBalloonHelp:    // we have a custom ballon help string
  70.             SBGetDetachedIndString(helpString, (*(MyGlobalHandle) params)->myStrings, kHelpStringIndex);
  71.             SBShowHelpString(statusRect, helpString);
  72.         break;
  73.     }
  74.     return (result);
  75. }
  76.  
  77. // ---------------------------------------------------------------------------
  78. //        • DoCSClick
  79. // ---------------------------------------------------------------------------
  80. //
  81. // handle a click in our status rect
  82. //
  83. void DoCSClick(MyGlobalHandle myGlobals, const Rect    *statusRect)
  84. {
  85.     short                menuID;
  86.  
  87.     OSErr                err = noErr;
  88.     GDHandle            gdh = GetMainDevice();
  89.     
  90.     if ((*myGlobals)->myMenuH){
  91.  
  92.         HLock((Handle)myGlobals);
  93.     
  94.         if ((*myGlobals)->myMenuH){
  95.             
  96.             // enable/disable/mark menu items
  97.             
  98.             menuID = SBTrackPopupMenu(statusRect, (*myGlobals)->myMenuH);
  99.             
  100.             switch (menuID){
  101.                 case 1:
  102.                     SysBeep(1);
  103.                     break;
  104.                 case 2:
  105.                     SysBeep(1);
  106.                     SysBeep(1);
  107.                     break;
  108.                 case 3:
  109.                     SysBeep(1);
  110.                     SysBeep(1);
  111.                     SysBeep(1);
  112.                     break;
  113.                 case 4:
  114.                     SysBeep(1);
  115.                     SysBeep(1);
  116.                     SysBeep(1);
  117.                     SysBeep(1);
  118.                     break;
  119.             }//end switch
  120.         }
  121.         
  122.         HUnlock((Handle)myGlobals);
  123.     }
  124. }
  125.  
  126. // ---------------------------------------------------------------------------
  127. //        • DoCSInit
  128. // ---------------------------------------------------------------------------
  129. //
  130. // allocate memory for our globals and init our globals
  131. //
  132. long DoCSInit(void)
  133. {
  134.     MyGlobalHandle    myH;
  135.     OSErr            iErr;
  136.     Handle            iconSuite = 0L;
  137.     
  138.     //
  139.     // get some memory
  140.     //
  141.     myH = (MyGlobalHandle) NewHandleClear(sizeof(MyGlobals));
  142.     
  143.     //
  144.     // got it
  145.     //
  146.     if (myH){    
  147.         HLock((Handle)myH);
  148.  
  149.     //
  150.     // get our icon suite
  151.     //
  152.         iErr = SBGetDetachIconSuite(&iconSuite, 256, 0x0000FF00L);
  153.         if (iErr) return iErr;
  154.         (*myH)->iconSuite = iconSuite;
  155.     //
  156.     // get our string list
  157.     //
  158.         (*myH)->myStrings = GetResource('STR#', 256);
  159.         iErr = ResError();
  160.         if (iErr) return iErr;
  161.         DetachResource((*myH)->myStrings);
  162.         iErr = ResError();
  163.         if (iErr) return iErr;
  164.     
  165.     //
  166.     // get our popup menu
  167.     //
  168.         (*myH)->myMenuH = GetMenu(kPopupMenuID);
  169.         if (!(*myH)->myMenuH) return ResError();
  170.         DetachResource((Handle)(*myH)->myMenuH);
  171.         iErr = ResError();
  172.         if (iErr) return iErr;
  173.     //
  174.     // get our arrow picture
  175.     //
  176.         (*myH)->myArrowPict = GetPicture(kArrowPictID);
  177.         if (!(*myH)->myArrowPict) return ResError();
  178.         DetachResource((Handle)(*myH)->myArrowPict);
  179.         iErr = ResError();
  180.         if (iErr) return iErr;
  181.         HUnlock((Handle)myH);
  182.         
  183.         return ((long) myH);
  184.     }
  185.     //
  186.     // did not get any memory, don't install
  187.     //
  188.     return (-1L);
  189. }
  190.  
  191. // ---------------------------------------------------------------------------
  192. //        • DoCSClose
  193. // ---------------------------------------------------------------------------
  194. //
  195. // free memory for our globals
  196. //
  197. void DoCSClose(MyGlobalHandle myGlobals)
  198. {
  199.     if (myGlobals){
  200.         HLock((Handle)myGlobals);
  201.         if ((*myGlobals)->iconSuite)
  202.             DisposeIconSuite ((*myGlobals)->iconSuite, true);
  203.         if ((*myGlobals)->myMenuH)
  204.             DisposeMenu((*myGlobals)->myMenuH);
  205.         if ((*myGlobals)->myStrings)
  206.             DisposeHandle((*myGlobals)->myStrings);
  207.         if ((*myGlobals)->myStrings)
  208.             KillPicture((*myGlobals)->myArrowPict);
  209.         DisposeHandle((Handle) myGlobals);
  210.     }
  211. }
  212.  
  213. // ---------------------------------------------------------------------------
  214. //        • DoCSDraw
  215. // ---------------------------------------------------------------------------
  216. //
  217. // draw our icon and arrow pict
  218. //
  219. void DoCSDraw(MyGlobalHandle myGlobals, Rect *statusRect, GrafPtr /*statusPort*/)
  220. {
  221.     Rect        viewRect;
  222.     short        alignment = 0x01 + 0x04; // center up & down , and left & right
  223.     long        transform = 0;              // no transform
  224.     short        arrowHeight = 0;
  225.     
  226.     if ((*myGlobals)->iconSuite){
  227.     //
  228.     // Draw our icon
  229.     //
  230.         viewRect = *statusRect;
  231.         viewRect.right = viewRect.left + kIconWidth;
  232.         (void)PlotIconSuite(&viewRect,atNone,ttNone,(*myGlobals)->iconSuite);
  233.     }
  234.     
  235.     //
  236.     // Draw our right-arrow pict to show that we have a popup menu
  237.     //
  238.     if ((*myGlobals)->myArrowPict){
  239.         arrowHeight = height((*(*myGlobals)->myArrowPict)->picFrame);
  240.         viewRect.left = viewRect.right;
  241.         viewRect.right += width((*(*myGlobals)->myArrowPict)->picFrame);
  242.         viewRect.top += ((height(viewRect) - arrowHeight) >> 1);
  243.         viewRect.bottom = viewRect.top + arrowHeight;
  244.         DrawPicture((*myGlobals)->myArrowPict, &viewRect);
  245.     }
  246. }